/*!
 **************************************************************
 *                                                            *  
 *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *  
 *    |                     ______                     |      *  
 *    |                  .-"      "-.                  |      *  
 *    |                 /            \                 |      *  
 *    |     _          |              |          _     |      *  
 *    |    ( \         |,  .-.  .-.  ,|         / )    |      *  
 *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *  
 *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *  
 *    |           "=._"(_     ^^     _)"_.="           |      *  
 *    |               "=\__|IIIIII|__/="               |      *  
 *    |              _.="| \IIIIII/ |"=._              |      *  
 *    |    _     _.="_.="\          /"=._"=._     _    |      *  
 *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *  
 *    |    > _.="                            "=._ <    |      *  
 *    |   (_/                                    \_)   |      *  
 *    |                                                |      *  
 *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *  
 *                                                            *  
 *           LASCIATE OGNI SPERANZA, VOI CH'ENTRATE           *  
 **************************************************************   
 ******************************************************************
 *  项目名称 : proxy
 *  文件描述 :
 *  文件名称 : buffer.js
 *  版  本  : v1.0.0.0
 *  说  明  :  
 *  作  者  : night
 *  邮  箱  : 88962800@qq.com
 *  创建时间 : 2019:01-07 14:28 
 ******************************************************************
 Copyright © 2017-2019, All rights reserved.
 ******************************************************************
 */
import EventEmitter from 'events';
export default class Buffer extends EventEmitter {
    constructor() {
        super();

    }

    buffer = [];
    index = 0;
    pos = 0;
    current = [];
    asyncFn;
    push(data){
        this.buffer.push(data);
        if(this.asyncFn){
            let line = this.getLine();
            if(line instanceof Array){
                line = this.toString(line);
                this.asyncFn = null;
                this.readLineSuccess(line);
                this.readLineSuccess=null;
                this.readLineError = null;

            }
        }
    }
    getLine() {
        for (; this.index < this.buffer.length; this.index++) {
            for (; this.pos < this.buffer[this.index].length; this.pos++) {
                let num = this.buffer[this.index][this.pos];
                if (num == 13)continue;
                if (num == 10) {
                    let n = this.current;
                    this.current = [];
                    ++this.pos;
                    return n;
                } else {
                    this.current.push(num);
                }
            }
            this.pos = 0;
        }
    }

    toString(buffer) {
        return String.fromCharCode.apply(String, buffer);
    }
    readLineSync(){
        let line = this.getLine();
        if (line instanceof Array)return this.toString(line);
        return null;
    }
    async readLine() {
        if (this.asyncFn)return await this.asyncFn;
        let line = this.getLine();
        if (line instanceof Array)return this.toString(line);
        this.asyncFn = new Promise((ps,pe)=>{
            this.readLineSuccess = ps;
            this.readLineError = pe;
        });
        return await this.asyncFn;
    }
}